package com.zx.idc.backend.gui.sys;


import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.common.constants.ISMSPriorityConstant;
import com.zx.idc.common.exception.CallInternalInterfaceException;
import com.zx.idc.common.exception.EncryptException;
import com.zx.idc.ds.sys.entity.SysBlackWhiteList;
import com.zx.idc.ds.sys.service.ISysBlackWhiteListService;
import com.zx.idc.ds.sys.vo.SysBlackWhiteListVo;
import com.zx.idc.internal.exception.SendMessageException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 黑白名单管理 前端控制器
 * </p>
 *
 * @author chendong
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/sysBlackWhiteList")
public class SysBlackWhiteListController extends BaseApi implements BaseExcelApi<ISysBlackWhiteListService, com.zx.idc.ds.sys.vo.SysBlackWhiteListVo> {

    @Autowired
    private ISysBlackWhiteListService service;

    @GetMapping("/query-page")
    @RequiresPermissions("system-configuration:sys-black-white-list")
    public Result<Page<com.zx.idc.ds.sys.vo.SysBlackWhiteListVo>> page(Page<com.zx.idc.ds.sys.vo.SysBlackWhiteListVo> page, com.zx.idc.ds.sys.vo.SysBlackWhiteListVo sysBlackWhiteListVo) {
        return success(service.pageByParam(page, sysBlackWhiteListVo));
    }

    @PostMapping("/update")
    @RequiresPermissions("system-configuration:sys-black-white-list:edit")
    public Result<Boolean> update(@RequestBody SysBlackWhiteListVo sysBlackWhiteList) {
        sysBlackWhiteList.setPriority(ISMSPriorityConstant.PUBLIC_BINARY_PRIORITY)
            .setEditable(SysBlackWhiteList.READ_WRITE);
        Exception ex;
        String errorMsg;
        try {
            return success(service.updateData(sysBlackWhiteList));
        } catch (SendMessageException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (EncryptException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (CallInternalInterfaceException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (Exception e) {
            ex = e;
            errorMsg = "未知错误";
        }
        log.error(errorMsg, ex);
        return error(errorMsg);
    }

    @PostMapping("/add")
    @RequiresPermissions("system-configuration:sys-black-white-list:edit")
    public Result<Boolean> insert(@RequestBody SysBlackWhiteListVo sysBlackWhiteList) {
        sysBlackWhiteList.setPriority(ISMSPriorityConstant.PUBLIC_BINARY_PRIORITY)
            .setEditable(SysBlackWhiteList.READ_WRITE)
            .setCreateTime(LocalDateTime.now())
            .setId(IdWorker.getId());
        Exception ex;
        String errorMsg;
        try {
            return success(service.insertData(sysBlackWhiteList));
        } catch (CallInternalInterfaceException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (EncryptException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (SendMessageException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (Exception e) {
            ex = e;
            errorMsg = "未知错误";
        }
        log.error(errorMsg, ex);
        return error(errorMsg);
    }

    /**
     * 通用的批量删除方法
     *
     * @param idList 实体主键列表
     * @return 批量删除是否成功，需要以 {@link Result#getData()} 作为判断标准
     */
    @PostMapping("/delete/batch")
    @RequiresPermissions("system-configuration:sys-black-white-list:edit")
    public Result<Boolean> delete(@RequestBody List<Long> idList) {

        List<SysBlackWhiteList> sysBlackWhiteLists = service.selectBatchIds(idList);
        if (sysBlackWhiteLists.isEmpty()) {
            return success(true);
        }
        List<SysBlackWhiteList> filterData = sysBlackWhiteLists
            .stream()
            .filter(obj -> obj.getEditable().equals(SysBlackWhiteList.READ_WRITE))
            .collect(Collectors.toList());
        Exception ex;
        String errorMsg;
        try {
            return success(service.deleteBatchData(filterData));
        } catch (CallInternalInterfaceException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (EncryptException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (SendMessageException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (Exception e) {
            ex = e;
            errorMsg = "未知错误";
        }
        log.error(errorMsg, ex);
        return error(errorMsg);
    }

    /**
     * 通用的删除方法
     *
     * @param id 实体主键
     * @return 删除是否成功，需要以 {@link Result#getData()} 作为判断标准
     */
    @PostMapping("/delete/{id}")
    @RequiresPermissions("system-configuration:sys-black-white-list:edit")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        SysBlackWhiteList sysBlackWhiteList = service.selectById(id);
        if (sysBlackWhiteList == null || sysBlackWhiteList.getEditable().equals(SysBlackWhiteList.ONLY_READ)) {
            return success(true);
        }
        Exception ex;
        String errorMsg;
        try {
            return success(service.deleteData(sysBlackWhiteList));
        } catch (CallInternalInterfaceException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (EncryptException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (SendMessageException e) {
            ex = e;
            errorMsg = e.getMessage();
        } catch (Exception e) {
            ex = e;
            errorMsg = "未知错误";
        }
        log.error(errorMsg, ex);
        return error(errorMsg);
    }
}

